extends KinematicBody2D

const canMoveRange = preload("res://Scene/Map/UI/MoveRange.tscn")
const attackRange = preload("res://Scene/Map/UI/AttackRange.tscn")

enum ActorState {
	Idle,Moving,Wait
}

var actor_state = ActorState.Idle
var movePoint   = Vector2.ZERO
var velocity    = Vector2.ZERO

var speed : int = 100

var movePath = []
var isArrive = false

func setActorAnim(dir:Vector2,sprite:AnimatedSprite,move_pos:Vector2,idle_pos:Vector2):
	if dir == Vector2(0,-1):
		sprite.play("MoveUp")
		sprite.position = move_pos
	elif dir == Vector2(0,1):
		sprite.play("MoveDown")
		sprite.position = move_pos
	elif dir == Vector2(1,0):
		sprite.play("MoveRight")
		sprite.position = move_pos
	elif dir == Vector2(-1,0):
		sprite.play("MoveLeft")
		sprite.position = move_pos
	elif dir == Vector2(-1,-1) :
		sprite.play("GrayIdle")
		sprite.position = idle_pos
	else :	
		sprite.play("Idle")
		sprite.position = idle_pos

func cleanActorPath(moveTileContainer:Node2D,attackTileContainer:Node2D):
	for tile in moveTileContainer.get_children():
		tile.queue_free()
		Game.canMovePathArr = []
	for attackTile in attackTileContainer.get_children():
		attackTile.queue_free()
		Game.attackPathArr = []
	Game.game_state = Game.GAMESTATE.IDLE

func showActorMovePath(moveTileContainer:Node2D,heroSpawn:Vector2,move_range:int):
	if Game.game_state == Game.GAMESTATE.SELECTMOVEPATH :
		Game.canMovePathArr = []
		var currentCanMovePath = []
		for i in range(-move_range,move_range+1):
			for j in range(-move_range,move_range+1):
				if abs(i) + abs(j) <= move_range :
					#将英雄到可行路径放入数组
					currentCanMovePath.append(Vector2(j,i))
					Game.canMovePathArr.append(Vector2(j+heroSpawn.x,i+heroSpawn.y))
					
		for canWalkTile in currentCanMovePath:
			var moveTile = canMoveRange.instance()
			moveTile.global_position = (heroSpawn+canWalkTile) * Config.size
			AStarPath.clear()
			AStarPath.used_cells = Game.canMovePathArr;
			AStarPath._add_points()
			AStarPath._connect_points()
			moveTileContainer.add_child(moveTile)

func showActorAttackPath(attackTileContainer:Node2D,heroSpawn:Vector2,attack_range:int):
	if Game.game_state == Game.GAMESTATE.SELECTATTACKPATH:
		Game.attackPathArr = []
		var currentAttackPath = []
		for i in range(-attack_range,attack_range+1):
			for j in range(-attack_range,attack_range+1):
				if abs(i) + abs(j) <= attack_range :
					#将英雄到可行路径放入数组
					currentAttackPath.append(Vector2(j,i))
					Game.attackPathArr.append(Vector2(j+heroSpawn.x,i+heroSpawn.y))
		for canAttackTile in currentAttackPath:
			var attackTile = attackRange.instance()
			attackTile.global_position = (heroSpawn+canAttackTile) * Config.size
			attackTileContainer.add_child(attackTile)
